from rest_framework import serializers
from rest_framework.validators import UniqueValidator

from apps.steps.models import Steps
from apps.case.models import Case


class StepListSerializers(serializers.ModelSerializer):
    step_num = serializers.IntegerField(read_only=True)

    class Meta:
        model = Steps
        fields = ('id', 'step_num', 'type', 'name', 'params', 'return_values', 'add_time', 'edit_time')


class PreStepListSerializers(serializers.ModelSerializer):

    class Meta:
        model = Steps
        fields = ('id', 'step_num', 'type', 'name', 'params', 'return_values')


class StepDetailSerializers(serializers.ModelSerializer):
    params = serializers.CharField()

    class Meta:
        model = Steps
        fields = ('id', 'case', 'name', 'step_num', 'step_info', 'params', 'type', 'return_values', 'expect')


class StepCreateSerializer(serializers.ModelSerializer):
    step_num = serializers.IntegerField(read_only=True)
    params = serializers.CharField()

    class Meta:
        model = Steps
        fields = '__all__'

    def validate(self, attrs):
        last_step_num = Steps.objects.filter(case=attrs['case'])
        if len(last_step_num):
            num = last_step_num[0].step_num
            print('num:', num)
            attrs['step_num'] = num+1
            return attrs
        else:
            return attrs
